// 云函数入口文件
const cloud = require('wx-server-sdk')
const {
  WXMINIUser,
  WXMINIMessage
}= require('wx-js-utils')
const appId = 'wx83d425ee01e0ca46'; // 从公众开放平台中获取appId
const secret = 'dd9f8569156f0a1a6db7cfb679728b34'; //从公众开放平台中获取secret
const template_id = 'iQpYQYqQC50utEA3QgmBADg_MYecWagfLSvY7-PkEhU'
cloud.init({
  env: 'database-2p2zg',
  traceUser:true
})

// 云函数入口函数
exports.main = async (event, context) => {

  let wXMINIUser = new WXMINIUser({
    appId,
    secret
  });

  let access_token = await wXMINIUser.getAccessToken();
  const touser = event.openid;
  const form_id = event.formId;
  let wXMINIMessage = new WXMINIMessage();
  console.log('form_id:',form_id,'touser:',touser,'access_token:',access_token)
  let result = await wXMINIMessage.sendMessage({
    access_token,
    touser,
    form_id,
    template_id,
    data:{
      keyword1:{
        value:event.who
      },
      keyword2:{
        value:event.content
      }
    },
    page: `pages/details/details?id=${event.pageID}`
  });
  return result;
}